﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Mang24k99.Business.COMM;
using Mang24k99.Model.COMM;
using System.Text.RegularExpressions;

namespace Mang24k99.Web.OrgList
{
    public partial class BankSort : System.Web.UI.Page
    {
         protected void Page_Load(object sender, EventArgs e)
        {
            if(!IsPostBack)
            {
                Bindate();
            }
            
        }
        
        //绑定数据
        public void Bindate()
        {
            string where = "orglist.ParentNO=3 order by orglist.LevelOrder";
            MKtCommOrglist[] orglist = (MKtCommOrglist[])BKtCommOrglist.SelectByWhere(where);
            this.grdList.DataSource = orglist;
            this.grdList.DataBind();
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            Bindate();
        }
        /// <summary>
        /// 排序
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnOK_Click(object sender, EventArgs e)
        {
            int count=this.grdList.Rows.Count;
            for (int i = 0; i < count;i++ )
            {
                int NO=Int32.Parse(this.grdList.Rows[i].Cells[1].Text);
                MKtCommOrglist orglist = BKtCommOrglist.Find(NO);
                GridViewRow gvr = grdList.Rows[i];
                HtmlInputText inputtext = (HtmlInputText)gvr.FindControl("txtsort");
                if (inputtext.Value == "")
                {
                    string strMsg = "排序号不能为空，位置第" + (i + 1) + "行！";
                    Common.MessageBox.Show(this.Page, strMsg);
                    return;
                }
                else
                {
                    string reg = @"^[1-9]\d*$";
                    Regex regex = new Regex(reg);
                    if (regex.IsMatch(inputtext.Value) == false)
                    {
                        string strMsg = "排序号必须为正整数，位置第" + (i + 1) + "行！";
                        Common.MessageBox.Show(this.Page, strMsg);
                        return;
                    }
                    if (Convert.ToInt32(inputtext.Value) == 0)
                    {
                        string strMsg = "排序号不能为0，位置第" + (i + 1) + "行！";
                        Common.MessageBox.Show(this.Page, strMsg);
                        return;
                    }
                }
              
                orglist.LevelOrder = Convert.ToInt32(inputtext.Value);
                try
                {
                    BKtCommOrglist.Update(orglist);
                }
                catch (System.Exception ex)
                {
                    throw;
                }
            }
            string Msg = "排序成功！";
            Common.MessageBox.Show(this.Page, Msg);
            Bindate();
        }

 
    }
 }
